import { serviceSimple } from "@/api"
import {Nanoid} from '@/utils/nanoid_token'
export default {
namespaced:true,
actions: {
   async getGoodsList({commit}){
        let res=await serviceSimple({url:'/cart/cartList'})   
       
        commit('GETGOODSLIST',res.data.data)
   },

   async updataGoodNum({commit},{skuId,skuNum}){
        await serviceSimple({url:`/cart/addToCart/${skuId}/${skuNum}`,method:'post'})
        
   },
   
   async deleteCart({commit},skuId){
    let res=await serviceSimple({url:`/cart/deleteCart/${skuId}`,method:'delete'})
   
    if(res.data.code==200) return Promise.resolve('删除成功')
    else return Promise.reject('删除失败')
    
   },


   async changeCheck(state,{skuID,isChecked}){
    let res=await serviceSimple({url:`/cart/checkCart/${skuID}/${Number(isChecked)}`})
    if(res.data.code==200) return Promise.resolve('更新成功')
    else return Promise.reject('更新失败')
    
   }
},
mutations: {
    GETGOODSLIST(state,goodList){
         if(goodList.length==0) return  state.shopcartList=[]
        else return state.shopcartList=goodList[0].cartInfoList

    }
},
state: {
    Nanoid:Nanoid(),
    shopcartList:[]
},
getters:{
 
}
}